package com.stack.knowyouweb.controller;


import com.google.gson.Gson;

import com.stack.knowyoubase.constant.GlobalConst;
import com.stack.knowyoubean.bean.Answer;
import com.stack.knowyoubean.bean.RepJson;
import com.stack.knowyouservice.service.AnswerService;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * 作业表响应层
 *
 * @author stack
 */
@RestController
@Scope("singleton")
@RequestMapping("/api")
public class AnswerController {

    @Resource
    private AnswerService answerService;
    @Resource
    private Gson gson;

    /**
     * 学生查看作业具体信息
     *
     * @param hid            作业号
     * @param session        session
     * @param authentication 认证角色
     * @return 学生作答的信息
     */
    @GetMapping("/answer/{hid}")
    public String answerInfo(@PathVariable("hid") Integer hid, HttpSession session, Authentication authentication) {
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            RepJson<String> repJson = new RepJson<>();
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        }
        //通过认证的角色和session域中的课程号保证信息安全
        Answer answer = answerService.answerInfo(hid, Integer.parseInt(authentication.getName()), (Integer) session.getAttribute("courseid"));
        RepJson<Answer> repJson = new RepJson<>();
        repJson.setCode(GlobalConst.SUCCESS_CODE);
        repJson.setData(answer);
        return gson.toJson(repJson);
    }

    /**
     * 作业表更新（学生与教师）
     *
     * @param answer         答案
     * @param session        session
     * @param authentication 认证角色
     * @return 作业更新
     */
    @PutMapping("/answer")
    public String answerUpdate(Answer answer, HttpSession session, Authentication authentication) {
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            RepJson<String> repJson = new RepJson<>();
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        }
        answer.setSid(sid);
        //通过认证的角色的信息进行更新
        HashMap<String, Object> hashMap = answerService.answerUpdate(answer, authentication);
        RepJson<String> repJson = new RepJson<>();
        repJson.setCode(GlobalConst.SUCCESS_CODE);
        //处理魔法值问题
        String flag = "flag";
        if ((boolean) hashMap.get(flag)) {
            repJson.setData(GlobalConst.SUCCESS_TYPE);
            repJson.setMsg((String) hashMap.get("msg"));
        } else {
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg((String) hashMap.get("msg"));
        }
        return gson.toJson(repJson);
    }

    /**
     * 教师查看学生作答信息
     *
     * @param hid     作业号
     * @param stuid   学生号
     * @param session session
     * @return 学生答案
     */
    @GetMapping("/answer/info")
    public String answerInfoForTc(Integer hid, Integer stuid, HttpSession session) {
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            RepJson<String> repJson = new RepJson<>();
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        }
        Answer answer = answerService.answerInfo(hid, stuid, (Integer) session.getAttribute("courseid"));
        RepJson<Answer> repJson = new RepJson<>();
        repJson.setCode(GlobalConst.SUCCESS_CODE);
        repJson.setData(answer);
        return gson.toJson(repJson);
    }

    /**
     * 学生答案列表
     *
     * @param hid     作业号
     * @param stuname 学生名
     * @param score   成绩
     * @param stuid   学号
     * @param page    页码
     * @param limit   条数
     * @param session session
     * @return 学生作答列表
     */
    @GetMapping("/answer/hid/{hid}")
    public String answerList(@PathVariable("hid") Integer hid, String stuname, Integer score, Integer stuid, Integer page, Integer limit, HttpSession session) {
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            RepJson<String> repJson = new RepJson<>();
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        }
        HashMap<String, Object> answerMap = answerService.answerList(hid, stuname, score, stuid, page, limit);
        RepJson<List<Answer>> repJson = new RepJson<>();
        if (answerMap == null) {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(null);
            repJson.setCount(0);
            repJson.setMsg("查询成功");
        } else {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData((List<Answer>) answerMap.get("answerList"));
            repJson.setCount((Integer) answerMap.get("count"));
            repJson.setMsg("查询成功");
        }
        //通过认证的角色和session域中的课程号保证信息安全
        return gson.toJson(repJson);
    }


}
